package com.ericsson.eritask;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ericsson.eritask.dao.impl.ProjectDaoImpl;
import com.ericsson.eritask.domain.Project;
import com.ericsson.eritask.domain.User;
import com.ericsson.eritask.domain.UserProject;
import com.ericsson.eritask.dto.MyTaskDTO;
import com.ericsson.eritask.dto.ProjectDTO;
import com.ericsson.eritask.service.MyTaskService;
import com.ericsson.eritask.service.ProjectService;
import com.ericsson.eritask.service.UserService;

@Controller
public class ProjectController {

	@Autowired
	ProjectService projectService;
	
	@Autowired
	MyTaskService myTaskService;
	
	@Autowired
	UserService userService;

	public void setMyTaskService(MyTaskService myTaskService) {
		this.myTaskService = myTaskService;
	}

	
	
	 public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	
	 @RequestMapping(value = "/getUsers", method = RequestMethod.GET,produces = { MediaType.APPLICATION_JSON_VALUE })
	 public @ResponseBody List<String> getUserList(@RequestParam("projectId") Integer projectID, HttpServletRequest request) {
		 System.out.println("calling project controller for user....");
		 
		// ProjectDaoImpl pdI = new ProjectDaoImpl();
		
		// System.out.println(pdI.getAllProjects());
		 System.out.println("projectId in userlist:::::::::::::::::::::::::::::::::"+projectID);
		 List<String> ulist = projectService.getUserList(projectID);
		// String signum = request.getSession().getAttribute("user_signum").toString();
		 //List<String> ls = projectService.getProjectListUser(signum);
		 //List<ProjectDTO> ls = projectService.getProjectListUser(signum);
		 
		 //projectService.getProjectList();
		 
		 System.out.println("User list in project controller"+ ulist);
		 
		 return ulist;
		 
	        
	    }
	 
	 

	 @RequestMapping(value = "/getProjects", method = RequestMethod.GET,produces = { MediaType.APPLICATION_JSON_VALUE })
	 public @ResponseBody List<ProjectDTO> getProjectList(HttpServletRequest request) {
		 System.out.println("calling project controller....");
		// ProjectDaoImpl pdI = new ProjectDaoImpl();
		 
		// System.out.println(pdI.getAllProjects());
		// List<String> ls = projectService.getProjectList();
		 Authentication auth= SecurityContextHolder.getContext().getAuthentication();

			System.out.format("Name of the user is %s", auth.getName());
			User user = userService.findByName(auth.getName());
			String signum = user.getSignum();
		 //String signum = request.getSession().getAttribute("user_signum").toString();
		 //List<String> ls = projectService.getProjectListUser(signum);
		 List<ProjectDTO> ls = projectService.getProjectListUser(signum);
		 
		 //projectService.getProjectList();
		 
		 System.out.println("in project controller"+ ls);
		 
		 return ls;
		 
	        
	    }
	
	
	 
	 @RequestMapping(value = "/getProjectTask", method = RequestMethod.GET)
	 public  String getTaskListByProjectId(HttpServletRequest request,@RequestParam("prjectId") String projectID,Model model) {
		 
		 System.out.println("entering to getTaskListByProjectId");
		 System.out.println("getProjectTask request mapping.calling...");
		 System.out.println("projectID:"+projectID);
		 int projectId= 0;
		 if(projectID!=null ){
			 projectId = Integer.valueOf(projectID);
		 
		 }
		 
			Map<Integer , List<MyTaskDTO>> project_taskMap = new HashMap<Integer, List<MyTaskDTO>>();
			String user_name = request.getSession().getAttribute("user_Name").toString();
			String password = request.getSession().getAttribute("passWord").toString();
			
			System.out.println("u = "+user_name+" "+" p = "+password);
			
			List<MyTaskDTO> taskDTO =null;
			List<MyTaskDTO> allTaskList = new ArrayList<MyTaskDTO>();
			
			 List<UserProject> userproject_list  =  myTaskService.getUserProjectList(user_name, password);
			  if(userproject_list!=null && userproject_list.size()>0){
				  for(UserProject up :userproject_list){
					  
					 int  proj_id = up.getProject().getProjectId();
					 System.out.println("proj_id in controller "+proj_id);
					  taskDTO = myTaskService.populateMyTaskList(proj_id);
					  project_taskMap.put(proj_id, taskDTO);
					 
				  }
			  }
			  
			  if(project_taskMap!=null && project_taskMap.size()>0){
				  List<MyTaskDTO> task_list =  project_taskMap.get(projectId);  
				  model.addAttribute("mytaskDTO", task_list);
			   }
			  
			  
				
	        return "MyTask";
	    }
	
}
